const pool = require("../connection/sqlPool");

async function GETUSER (){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.query("select * from `Wx_user`order by `top` desc");
        return rows;
    }catch(e){
        return e.message;
    }
}

async function GETLINE(){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.query("select * from `Route_line`");
        return rows;
    }catch(e){
        return e.message;
    }
}

async function DELEUSER(id){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("delete from `Wx_user` where `ID` = ?",[id]);
        return {code:200,msg:"删除成功"}
    }catch(e){
        return e.message;
    }

}

async function DELETELINE(id){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("delete from `Route_line` where `ID` = ?",[id]);
        return {code:200,msg:"删除成功"}
    }catch(e){
        return e.message;
    }
}

async function UPDATEUSER(id,username,password){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("update `Wx_user` set `username` = ?,`password` = ? where `ID` = ?",[username,password,id]);
        return {code:200,msg:"修改成功"}
    }catch(e){
        return e.message;
    }
}

async function UPDATELINE(id,route,approach,address,telephone){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("update `Route_line` set `route` = ?,`approach` = ?,`address` = ?,`telephone` = ? where `ID` = ?",[route,approach,address,telephone,id]);
        return {code:200,msg:"修改成功"}
    }catch(e){
        return e.message;
    }
}

async function ADDUSER(username,password){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("insert into `Wx_user` (`username`,`password`) values (?,?)",[username,password]);
        return {code:200,msg:"添加成功"}
    }catch(e){
        return e.message;
    }
    
}

async function ADDLINE(route,approach,address,telephone){
    try{
        const promisePool = pool.promise();
        const [rows,fields] = await promisePool.execute("insert into `Route_line` (`route`,`approach`,`address`,`telephone`) values (?,?,?,?)",[route,approach,address,telephone]);
       return {code:200,msg:"添加成功"}
    }catch(e){
        return e.message;
    }
}

module.exports = {
    GETUSER,DELEUSER,UPDATEUSER,ADDUSER,
    GETLINE,DELETELINE,UPDATELINE,ADDLINE

}